# import matplotlib.pyplot as plt
# import numpy as np
# from scipy.stats import ttest_rel
# from scipy.stats import pearsonr

# import matplotlib.pyplot as plt


# task_3851 = [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
# task_5152 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0]
# task_7979 = [0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

# # 定义颜色映射
# colors = ['red', 'green', 'blue', 'yellow']

# # 绘制柱状图
# fig, ax = plt.subplots()
# for i, color_idx in enumerate(task_5152):
#     ax.bar(i, 1, color=colors[color_idx], width=0.8)

# # 设置 x 轴标签
# ax.set_xticks(range(len(task_5152)))
# ax.set_xticklabels(range(10, 10+len(task_5152)))

# # 显示 plot
# plt.show()

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from mpl_toolkits.mplot3d import Axes3D

# 洛伦兹系统参数
sigma = 10.0
rho = 28.0
beta = 8.0 / 3.0

# 定义洛伦兹系统的向量场函数
def lorenz_vector_field(x, y, z):
    dx = sigma * (y - x)
    dy = x * (rho - z) - y
    dz = x * y - beta * z
    return dx, dy, dz

# 定义空间范围
x_min, x_max = -20, 20
y_min, y_max = -30, 30
z_min, z_max = 0, 50

# 在Y和Z方向创建网格
y_points = np.linspace(y_min, y_max, num=20)
z_points = np.linspace(z_min, z_max, num=20)
Y, Z = np.meshgrid(y_points, z_points)

# 创建图形和轴
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')

# 调整子图以给滑块留出空间
plt.subplots_adjust(bottom=0.25)

# 初始平面位置
initial_percent = 50  # 初始位置为50%
initial_x0 = x_min + (initial_percent / 100.0) * (x_max - x_min)

# 绘制初始向量场
X0 = initial_x0 * np.ones_like(Y)
dx0, dy0, dz0 = lorenz_vector_field(X0, Y, Z)

vector_norm0 = np.sqrt(dx0**2 + dy0**2 + dz0**2)
vector_norm0[vector_norm0 == 0] = 1
dx_norm0 = dx0 / vector_norm0
dy_norm0 = dy0 / vector_norm0
dz_norm0 = dz0 / vector_norm0

# 绘制初始的三维向量场，并将Q存储在一个列表中
Q = [ax.quiver(X0, Y, Z, dx_norm0, dy_norm0, dz_norm0, length=1.5, normalize=True, color='blue')]

# 绘制初始的包裹截面的长方体盒子线框，并将其存储在列表lines中
lines = []
def draw_rectangle(x0):
    # 定义长方体的八个顶点
    y0_min, y0_max = y_min, y_max
    z0_min, z0_max = z_min, z_max
    delta_x = 0.1  # 盒子的厚度

    x_corners = [x0 - delta_x/2, x0 + delta_x/2]
    y_corners = [y0_min, y0_max]
    z_corners = [z0_min, z0_max]

    # 定义长方体的十二条边
    rectangle_lines = []

    # 下底面（z = z0_min）
    for i in range(2):
        xs = [x_corners[i], x_corners[i]]
        ys = [y_corners[0], y_corners[1]]
        zs = [z0_min, z0_min]
        line, = ax.plot(xs, ys, zs, color='red')
        rectangle_lines.append(line)

    # 上底面（z = z0_max）
    for i in range(2):
        xs = [x_corners[i], x_corners[i]]
        ys = [y_corners[0], y_corners[1]]
        zs = [z0_max, z0_max]
        line, = ax.plot(xs, ys, zs, color='red')
        rectangle_lines.append(line)

    # 侧边
    for i in range(2):
        for j in range(2):
            xs = [x_corners[i], x_corners[i]]
            ys = [y_corners[j], y_corners[j]]
            zs = [z0_min, z0_max]
            line, = ax.plot(xs, ys, zs, color='red')
            rectangle_lines.append(line)

    # 前后面
    for j in range(2):
        ys = [y_corners[j], y_corners[j]]
        zs = [z0_min, z0_max]
        xs = [x_corners[0], x_corners[1]]
        line, = ax.plot(xs, ys, zs, color='red')
        rectangle_lines.append(line)

    return rectangle_lines

# 绘制初始的长方体盒子线框
lines = draw_rectangle(initial_x0)

ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_zlim(z_min, z_max)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('x = {:.2f}'.format(initial_x0))

# 添加滑块
ax_slider = plt.axes([0.15, 0.1, 0.7, 0.03])
slider = Slider(ax_slider, 'position', valmin=0, valmax=100, valinit=initial_percent, valfmt='%0.0f%%')

# 更新函数
def update(val):
    # 从列表中获取当前的Quiver对象并移除
    Q[0].remove()  # 移除之前的向量场

    # 移除之前的长方体线框
    for line in lines:
        line.remove()
    lines.clear()

    # 获取滑块的值，并计算对应的x0
    percent = slider.val
    x0 = x_min + (percent / 100.0) * (x_max - x_min)

    # 计算在x = x0处的向量场
    X = x0 * np.ones_like(Y)
    dx, dy, dz = lorenz_vector_field(X, Y, Z)

    # 归一化向量以便更好地可视化
    vector_norm = np.sqrt(dx**2 + dy**2 + dz**2)
    vector_norm[vector_norm == 0] = 1
    dx_norm = dx / vector_norm
    dy_norm = dy / vector_norm
    dz_norm = dz / vector_norm

    # 在新的位置绘制向量场，并更新列表中的Quiver对象
    Q[0] = ax.quiver(X, Y, Z, dx_norm, dy_norm, dz_norm, length=1.5, normalize=True, color='blue')

    # 绘制新的长方体线框，并更新lines列表
    new_lines = draw_rectangle(x0)
    lines.extend(new_lines)

    # 更新坐标轴范围和标签
    ax.set_xlim(x_min, x_max)
    ax.set_ylim(y_min, y_max)
    ax.set_zlim(z_min, z_max)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_title('x = {:.2f}'.format(x0))

    # 重新绘制图形
    fig.canvas.draw_idle()

# 连接滑块和更新函数
slider.on_changed(update)

plt.show()